package demo1;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2023-11-18
 * Time: 14:49
 */
class Money implements Cloneable{
    public double money;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Money money1 = (Money) o;
        return Double.compare(money1.money, money) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(money);
    }
}

class Student implements Cloneable{
    public int age;

    public Money m = new Money();

    public Student(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student tmp = (Student) super.clone();
        tmp.m = (Money) this.m.clone();
        return tmp;
        // return super.clone();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(m, student.m);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, m);
    }
}

public class Test {

    public static void main(String[] args) {
        Student student1 = new Student(10);
        Student student2 = new Student(10);
        System.out.println(student1.hashCode());
        System.out.println(student2.hashCode());
    }

    public static void main3(String[] args) {
        Student student1 = new Student(10);
        Student student2 = new Student(10);
        System.out.println(student1 == student2);
        System.out.println(student1.equals(student2));

        String s = "123456";
        System.out.println(s.equals("123456"));
    }

    public static void main2(String[] args) throws CloneNotSupportedException{
        Student student1 = new Student(10);
        student1.m.money = 19.9;
        Student student2 = (Student) student1.clone();
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);

        System.out.println("=================");
        student1.m.money = 29.9;
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
    }


    public static void main1(String[] args) throws CloneNotSupportedException{
        Student student1 = new Student(10);
        Student student2 = (Student) student1.clone();
        System.out.println(student1);
        System.out.println(student2);
    }
}


/*class Person{}
class Student{}
public class Test {
    public static void main(String[] args) {
        function(new Person());
        function(new Student());
    }
    public static void function(Object obj) {
        System.out.println(obj);
    }
}*/


    /*// Object类中的toString()方法实现：
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }*/

    /*// Object类中的equals方法
    public boolean equals(Object obj) {
        return (this == obj); // 使用引用中的地址直接来进行比较
    }*/


/*
class Person{
    private String name ;
    private int age ;
    public Person(String name, int age) {
        this.age = age ;
        this.name = name ;
    }
}
public class Test {
    public static void main(String[] args) {
        Person p1 = new Person("gaobo", 20) ;
        Person p2 = new Person("gaobo", 20) ;
        int a = 10;
        int b = 10;
        System.out.println(a == b); // 输出true
        System.out.println(p1 == p2); // 输出false
        System.out.println(p1.equals(p2)); // 输出false
    }
}*/


/*
class Person{
...
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false ;
        }
        if(this == obj) {
            return true ;
        }
// 不是Person类对象
        if (!(obj instanceof Person)) {
            return false ;
        }
        Person person = (Person) obj ; // 向下转型，比较属性值
        return this.name.equals(person.name) && this.age==person.age ;
    }
}*/
